<?php

	defined( 'APP_PATH' )
		|| define( 'APP_PATH', levelUp( dirname( __FILE__ ) ) );
	defined( 'INCLUDE_PATH' )
		|| define( 'INCLUDE_PATH', APP_PATH . 'components' . DIRECTORY_SEPARATOR );
	defined( 'FRAMEWORK_PATH' )
		|| define( 'FRAMEWORK_PATH', APP_PATH . 'framework' . DIRECTORY_SEPARATOR );
	defined( 'PHP_PATH' )
		|| define( 'PHP_PATH', APP_PATH . 'php' . DIRECTORY_SEPARATOR );
	defined( 'TPL_PATH' )
		|| define( 'TPL_PATH', APP_PATH . 'tpl' . DIRECTORY_SEPARATOR );
	defined( 'PUBLIC_PATH' )
		|| define( 'PUBLIC_PATH', APP_PATH . 'public' . DIRECTORY_SEPARATOR );	
	defined( 'ADMIN_URL_PART' )
		|| define( 'ADMIN_URL_PART', 'admin' );
	defined( 'DEFAULT_ADMIN_PAGE' )
		|| define( 'DEFAULT_ADMIN_PAGE', 'team' );

	require_once( APP_PATH . 'config' . DIRECTORY_SEPARATOR . 'settings.php' );
	require_once( FRAMEWORK_PATH . 'functions.php' );
	require_once( INCLUDE_PATH . 'functions.php' );
	require_once( FRAMEWORK_PATH . 'Loader.php' );

	spl_autoload_register( null, false );
	spl_autoload_extensions( '.php' );
	spl_autoload_register( Array( 'Loader', 'includeClass' ) );
	register_shutdown_function( 'application_shutdown_handler' );
	ini_set( 'display_errors', $applicationLevelDebug ? 1 : 0 );

	function levelUp( $dir ) {
		$dir = trim( $dir, DIRECTORY_SEPARATOR );
		$path = explode( DIRECTORY_SEPARATOR, $dir );
		array_pop( $path );
		return ( DIRECTORY_SEPARATOR == '/' ? '/' : '' ) . implode( DIRECTORY_SEPARATOR, $path ) . DIRECTORY_SEPARATOR;
	}
	
	function wpLog( $thing ) {
		
	};

	function application_shutdown_handler() {
		global $applicationLevelDebug;
		if( !is_null( $lastError = error_get_last() ) && !$applicationLevelDebug ) {
			if( array_search( $lastError[ 'type' ], array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR ) ) !== false ) {
				//doInternaServerError();
				
			}
		}
	}

	try {
		
		$arrH = explode( '.', $_SERVER[ 'HTTP_HOST' ] );
		$lH = count( $arrH );
		$cookieDomain = '.' . $arrH[ $lH - 2 ] . '.' . $arrH[ $lH - 1 ];
		
		ini_set('session.cookie_domain', $cookieDomain);

		session_start();
		session_regenerate_id();

		$wtLoginUserId = 0;
		if( Login::checkLoginStatus() == Login::USER_LOGGED_IN ) {
			$wtLoginUserId = Login::getUserInfo( 'userId' );
		}

		Url::processUrl();
		$requireFile = Url::getUrlFileUri();
		$arrDomainUri = Url::getSplittedDomainURI();
		$hostName = Url::getHostName();
		$adminPage = Url::getAdminPage();
		
		$subDomain = Url::getsubDomainValue();
		
		defined( 'HOST_NAME' )
			|| define( 'HOST_NAME', $hostName );
		defined( 'HOME_PATH' )
			|| define( 'HOME_PATH', "http://$subDomain.$hostName/" );

		$fileName = explode( DIRECTORY_SEPARATOR, $requireFile );
		$tplFile = str_replace( DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR . 'tpl' . DIRECTORY_SEPARATOR, str_replace( '.php', '.tpl.php', $requireFile ) );
		if( file_exists( $tplFile ) ) {
			$template = new PHPTAL( $tplFile );
			require_once( $requireFile );
			echo $template->execute();
		} else {
			require_once( $requireFile );
		}
	} catch( Exception $ex ) {
		if( $applicationLevelDebug ) {
			describe( $ex->getMessage() . "\n" . $ex->getTraceAsString() );
		}
	}